/***************************************************************
 * Name:      wxLogTrace.cpp
 * Purpose:   Defines Application Frame
 * Author:    walnutcy (walnutcy@gmail.com)
 * Created:   2012-09-14
 * Copyright: walnutcy (weibo.com/walnutcy)
 * License:
 **************************************************************/
#include "wxLogTrace.h"
#include <wx/stattext.h>

extern void wxAfxLogFmt(u8 isThread, const wxChar* fmt, ...);

wxLogTrace::wxLogTrace()
{
    //ctor
    flg = 0;
    flgThread = 0;
}

wxLogTrace::~wxLogTrace()
{
    //dtor
}

int  wxLogTrace::createLogFile(wxString fileName, u8 isThread)
{
    filePath = fileName;
    if (flg)
    {
        file.Close();
        flg = 0;
    }
    if (0 == file.Open(fileName.GetData(), wxFile::write))
    {
        wxAfxLogFmt(isThread, _("Error Open Input File: %s\r\n"), (wxChar*)fileName.GetData());
        return -1;
    }
    flg = 1;
    flgThread = isThread;

    return 0;
}

void wxLogTrace::log(const wxChar* fmt, ...)
{
    if (!flg)  return;

    wxChar  buffer[1024];
    va_list va;

    va_start(va, fmt);
    wxVsnprintf(buffer, 1024, fmt, va);
    va_end(va);

    file.Write(buffer, ::wxStrlen(buffer)*sizeof(wxChar));
    wxAfxLogFmt(flgThread, buffer);
}

void wxLogTrace::close(void)
{
    if (flg)
    {
        file.Close();
        flg = 0;
    }
}
